<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./tools.js"></script>
</head>
<body>
    <input type="text" class="username">
    <input type="password" class="pwd">
    <input type="file" multiple class="img">
    <button>点击发送ajax</button>
    <script>
        let btn = document.querySelector("button");
        let userEle = document.querySelector(".username");
        let imgEle = document.querySelector(".img");
        let pwdEle = document.querySelector(".pwd");
        btn.onclick = function (){
            let form = new FormData();
            let img = [...imgEle.files];
            console.log(img);
            fileUploads(img,form);
            form.append("username",userEle.value);
            form.append("pwd",pwdEle.value);
            ajax({
                url:'test.php',
                method:'post',
                dataType:'text',
                contentType:false,
                data:form,
                success(res){
                    console.log(res);
                }
            });
        }
        function fileUploads(fileArr,form){
            fileArr.forEach(function(item,key){
                form.append("item"+key,item);
                console.log(key,item);
            });
            form.append("fileLen",fileArr.length);
            return form ; 
        }
    </script>
</body>
</html>